package org.joinme.backend.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.FilterRegistration.Dynamic;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
   
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { AppConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        //激活配置文件
        servletContext.setInitParameter("spring.profiles.active", "prod");

        Dynamic filter = servletContext.addFilter("loginFilter", LoginFilter.class);
        filter.addMappingForUrlPatterns(null, true, "/*");

        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        filter = servletContext.addFilter("encodingFilter", characterEncodingFilter);
        filter.addMappingForUrlPatterns(null, false, "/*");

        super.onStartup(servletContext);
    }
}  
